package org.darkforest.common.services.impl;

import java.util.List;

import org.darkforest.common.services.CountyService;
import org.darkforest.dao.mapper.CadreSupportPoorMapper;
import org.darkforest.dao.mapper.CountySupportPoorMapper;
import org.darkforest.model.CadreSupportPoor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("countyService")
public class CountyServiceImpl implements CountyService {

    @Autowired
    private CountySupportPoorMapper countyMapper;

    @Autowired
    private CadreSupportPoorMapper cadreMapper;

    @Override
    @Transactional
    public int deleteCounty(String[] countyId) {

        int delCount = 0;

        for (String id : countyId) {
            // 扶贫县干部的删除
            CadreSupportPoor cadre = new CadreSupportPoor();
            cadre.setCountyId(Integer.parseInt(id));
            List<CadreSupportPoor> cadreGiftList = cadreMapper.selectListByCondition(cadre);
            for (CadreSupportPoor cadreDb : cadreGiftList) {
                cadreMapper.deleteByPrimaryKey(cadreDb.getCadreId());
            }

            //  扶贫县的删除
            int j = countyMapper.deleteByPrimaryKey(Integer.parseInt(id));
            delCount = delCount + j;
        }

        if (delCount != countyId.length) {
            return 0;
        }

        return 1;
    }
}
